package morphing;

import java.awt.geom.Line2D;
import java.awt.image.BufferedImage;


import java.util.Iterator;
import java.util.ArrayList;


public class Morphing_ {
	private int transicoes;
	private BufferedImage img1;
	private BufferedImage img2;
	private ArrayList<Line2D.Double> vetor1;
	private ArrayList<Line2D.Double> vetor2;
	protected ArrayList<BufferedImage> resultado;
	
	
	public Morphing_(BufferedImage img1, BufferedImage img2, ArrayList<Line2D.Double> vetor1, ArrayList<Line2D.Double> vetor2, int transicoes, boolean holdEdges) {
		this.transicoes = transicoes;
		this.img1 = img1;
		this.img2 = img2;
		this.vetor1 = vetor1;
		this.vetor2 = vetor2;		
		
		if (holdEdges) {
			Line2D.Double bottom_left = 
				new Line2D.Double(0, 0, img1.getWidth(), 0);
			Line2D.Double top_left = 
				new Line2D.Double(img1.getWidth(), 0, img1.getWidth(), img1.getHeight());
			Line2D.Double top_right = 
				new Line2D.Double(img1.getWidth(), img1.getHeight(), 0, img1.getHeight());
			Line2D.Double bottom_right = 
				new Line2D.Double(0, img1.getHeight(),	0, 0);

			vetor1.add(bottom_left);
			vetor1.add(top_left);
			vetor1.add(top_right);
			vetor1.add(bottom_right);

			bottom_left = 
				new Line2D.Double(0, 0, img2.getWidth(), 0);
			top_left = 
				new Line2D.Double(img2.getWidth(), 0, img2.getWidth(), img2.getHeight());
			top_right = 
				new Line2D.Double(img2.getWidth(), img2.getHeight(), 0, img2.getHeight());
			bottom_right = 
				new Line2D.Double(0, img2.getHeight(), 0, 0);

			vetor2.add(bottom_left);
			vetor2.add(top_left);
			vetor2.add(top_right);
			vetor2.add(bottom_right);
		}
		
	}

	public ArrayList<BufferedImage> morph() throws Exception {

		
		
		ArrayList<BufferedImage> morps1;
		ArrayList<BufferedImage> morps2;
		Warping_ warp;
		Dissolve      disso;
		InterpolacaoVetorial inter;
		ArrayList<Line2D.Double>[] interpol;

		inter = new InterpolacaoVetorial(transicoes);						
		morps1 = new ArrayList<BufferedImage>();
		morps2 = new ArrayList<BufferedImage>();
		warp = new Warping_(false);
		disso = new Dissolve(transicoes);
		resultado = new ArrayList<BufferedImage>();				

		interpol = inter.geraInterpolacoes(vetor1, vetor2, img1, img2);

		for(int i=0; i<interpol.length;i++) {


			morps1.add(warp.warping(vetor1, interpol[i],img1));
			
			morps2.add(warp.warping(vetor2, interpol[i], img2));

		}

		Iterator iter = morps1.iterator();
		Iterator iter2 = morps2.iterator();
		for (int i=1; i<transicoes; i++) {
			resultado.add(disso.dissolve(i, (BufferedImage)iter.next(), (BufferedImage)iter2.next()));
		}

		resultado.add(0,img1);
		resultado.add(resultado.size(),img2);

		iter = resultado.iterator();
		
		return resultado;
	}
	
	
	
}
